package com.zsj.service_user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsj.common_util.R;
import com.zsj.service_user.entity.AppSocial;
import com.zsj.service_user.service.AppSocialService;
import com.zsj.service_user.service.AppUserService;
import com.zsj.service_user.util.TokenMd5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsj
 * @since 2022-05-05
 */
@RestController
@RequestMapping("/serviceUser/social")
@CrossOrigin
@Slf4j
public class AppSocialController {


    @Autowired
    private AppSocialService appSocialService;


    /**
     * 添加动态
     */
    @PostMapping("/addSocial")
    public R addSocial(@RequestParam("token")String token, @RequestBody AppSocial social){
        if (TokenMd5.getMD5(social.getSocialBelongtoUser()).equals(token)) {
            //说明验证通过,
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            social.setSocialCreateTime(format.format(new Date()));
            if (appSocialService.save(social)){
                return R.ok().message("添加成功");
            }else{
                log.error("添加失败");
                return R.error().message("添加失败");
            }
        }else return R.error().message("身份验证失败");
    }

    /**
     * 得到所有的动态
     */
    @GetMapping("/getAllSocial")
    public R getALLSocial(){
        List<AppSocial> list = appSocialService.list(new QueryWrapper<>());
        if (list.isEmpty()){
            log.error("在getAllSocial方法中查询的列表为空");
            return R.ok().message("数据为空").data("list",list);
        }else return R.ok().data("list",list);
    }



}

